package org.springblade.upgrade.upload.constant;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateUnit;

public class LocalCache {
    /**
     * 缓存时长
     */
    public static final long MINUTE_5 = 5 * DateUnit.MINUTE.getMillis();

    /**
     * 默认缓存时长
     */
    public static final long DEFAULT_TIMEOUT = 10 * DateUnit.MINUTE.getMillis();
    /**
     * 默认清理间隔时间
     */
    private static final long CLEAN_TIMEOUT = 5 * DateUnit.MINUTE.getMillis();
    /**
     * 缓存对象
     */
    private static final TimedCache<String, Object> TIMED_CACHE = CacheUtil.newTimedCache(DEFAULT_TIMEOUT);

    static {
        //启动定时任务
        TIMED_CACHE.schedulePrune(CLEAN_TIMEOUT);
    }

    /**
     * 赋值
     *
     * @param key
     * @param value
     */
    public static void set(String key, Object value) {
        TIMED_CACHE.put(key, value);
    }

    /**
     * 赋值
     *
     * @param key
     * @param value
     * @param expire
     */
    public static void set(String key, Object value, long expire) {
        TIMED_CACHE.put(key, value, expire);
    }

    /**
     * 获取
     *
     * @param key
     * @return
     */
    public static Object get(String key) {
        return TIMED_CACHE.get(key, false);
    }

    /**
     * 移除
     *
     * @param key
     */
    public static void remove(String key) {
        TIMED_CACHE.remove(key);
    }

    /**
     * 清理
     */
    public static void clear() {
        TIMED_CACHE.clear();
    }


}
